/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/
 
package testing;
import lights.adapters.*;
import lights.interfaces.*;

/**
 * This class tests the "matches" method of LighTS: the empty tuple (= match-all-tuples template according to LighTS 
 * specifications) is tested, too.
 * <br>
 * Optional user input: tuplespace adapter to test.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class LighTSMatches 
{
 /**
  * Main method.
  * 
  * @param args user input: (optional) name of the tuplespace adapter to test
  */	
 public static void main(String[] args)
 {	
  ITuple[] template = new ITuple[3];
  ITuple[] tuple = new ITuple[3];
  String[] msg = new String[3];
  String tsfactory;

  // if the user did not choose any tuplespace factory then we test the default tuplespace factory
  if (args != null && args.length != 1) tsfactory = "lights.adapters.builtin.TupleSpaceFactory";  
                                   else tsfactory = "lights.adapters." + args[0] + ".TupleSpaceFactory";
  
  // sets the tuplespace factory
  try 
  { 
   TupleSpaceFactory.setFactory(tsfactory); 
   System.out.println("tuplespace factory set to " + tsfactory + "\n");
  } 
  catch(ClassNotFoundException cnfe) 
  { 
   System.out.println("can't set tuplespace factory to " + tsfactory); 
   cnfe.printStackTrace();
   System.exit(-1); 
  }
  
  // textual representation of templates and tuples
  msg[0] = "<>";
  msg[1] = "<hello>";
  msg[2] = "<String>";
 
  // set the templates to test
  template[0] = new Tuple();
  template[1] = new Tuple().add(new Field().setValue("hello"));
  template[2] = new Tuple().add(new Field().setType(String.class));
 
  // set the tuples to test
  tuple[0] = new Tuple();
  tuple[1] = new Tuple().add(new Field().setValue("hello"));
  tuple[2] = new Tuple().add(new Field().setType(String.class));
 
  // prints results of testing
  for (int i = 0; i < template.length; i++)
  for (int j = 0; j < tuple.length; j++)
      System.out.println(msg[i] + (template[i].matches(tuple[j]) ? " == " : " != ") + msg[j]); 	
 } 

}